package com.controller.manager;

import java.io.OutputStream;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.po.DishBeanW;
import com.po.PageNumber;
import com.service.manager.DishServiceW;

@Controller
@RequestMapping("/jsp")
public class DishController {
	
	@Resource(name="dishServiceImpW")
	private DishServiceW dishService;
	
	@ResponseBody
	@RequestMapping(value="/getDishList")
	private Object getDishList(DishBeanW dish,Integer page,Integer rows){
		
		dish.setPage((page-1)*rows);
		dish.setRows(rows);
		List<DishBeanW> list = dishService.getDishList(dish);
		int count = dishService.getDishCount(dish);
		PageNumber beanpage = new PageNumber();
		beanpage.setRows(list);
		beanpage.setTotal(count);
		return beanpage;
	}
	
	@ResponseBody
	@RequestMapping(value="/updateDish")
	public int updateDish(DishBeanW dish){
		
		int result = dishService.updateDish(dish);
		return result;
	}
	
	@ResponseBody
	@RequestMapping(value="/delDishList")
	public int delDishList(String dishids){
		String[] dishid = dishids.split(",");
		int result = dishService.delDishList(dishid);
		return result;
	}
	
	@RequestMapping(value="/exportDish")
	public void exportExcel(String dishids,HttpServletResponse response) throws Exception{
		
		response.setHeader("Content-Disposition", "attachment;filename=dish.xls");
		response.setContentType("application/vnd.ms-excel;charset=UTF-8");// 定义输出类型
		System.out.println(dishids);
		String[] array = null;
		if(dishids !=null){
			 array = dishids.split(",");
		}
		List<DishBeanW> list = dishService.getAdminByids(array);
		HSSFWorkbook wb  = getWorkbook(list);
		OutputStream out = response.getOutputStream();
		wb.write(out);
		out.flush();
		out.close();
	}
	public HSSFWorkbook getWorkbook(List<DishBeanW> list){
		
		HSSFWorkbook wb = new HSSFWorkbook();//对应一个excel
		HSSFSheet sheet = wb.createSheet("系统菜单");//对应一个sheet
		HSSFRow row = sheet.createRow(0);//对应表头
		
        HSSFCellStyle style = wb.createCellStyle(); //对应样式
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        
        String[] titles = {"菜品编号","菜品名称","菜品介绍","菜品价格","做菜方式"};
        HSSFCell cell;
        for(int i = 0;i < 5;i++){
        	
        	cell = row.createCell(i);
        	cell.setCellValue(titles[i]);
            cell.setCellStyle(style);
        	
        }
        
        for(int i = 0;i < list.size();i++){
        	
          row = sheet.createRow(i+1);
          DishBeanW	dish = (DishBeanW)list.get(i);
          row.createCell(0).setCellValue(dish.getDishid());
          row.createCell(1).setCellValue(dish.getDishname());
          row.createCell(2).setCellValue(dish.getDishintro());
          row.createCell(3).setCellValue(dish.getDishprice());
          row.createCell(4).setCellValue(dish.getDishcookway());
        }
		return wb;
	}
}
